<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="代理" prop="keyword">
          <el-input v-model="filterForm.agent" placeholder="代理ID/代理名称/代理电话号码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="客户" prop="keyword">
          <el-input v-model="filterForm.clientete" placeholder="客户/客户名称/电话号码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="下单时间" prop="keyword">
          <el-date-picker 
            v-model="filterForm.happenDate"
            value-format="yyyy-MM-dd"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="keyword">
          <el-date-picker 
            v-model="filterForm.happenDate"
            value-format="yyyy-MM-dd"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="filterForm.amount" placeholder="金额" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="1" value="1">已结算</el-option>
              <el-option label="2" value="2">未结算</el-option>
              <el-option label="3" value="3">已废除</el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="编号" min-width="60px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleShow(row)">{{ row.orderCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品" min-width="150px">
        <template slot-scope="{row}">
          <span v-for="shopping in row.shopping" :key="shopping.ID">
            <el-avatar shape="square" :size="100" :fit="fit" :src="shopping.avatar" />
            {{ shopping.product.title }} <br>价格：{{ shopping.price }}-数量：{{ shopping.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" max-width="60px">
        <template slot-scope="{row}">
          <span>￥{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佣金" max-width="60px">
        <template slot-scope="{row}">
          <span>￥{{ row.commission }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户" max-width="60px">
        <template slot-scope="{row}">
          <span>{{ row.clientete.fullname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="代理" max-width="50px">
        <template slot-scope="{row}">
          <span>{{ row.agent.fullname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" max-width="50px">
        <template slot-scope="{row}">
          <span>{{ row.orderTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
  </div>
</template>

<script>
import { getOrders, changeStatusByOrderId, removeOrderById } from '@/api/distribution/order'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      filterForm: {
        start: 1,
        perpage: 10,
        keyword: undefined,
        settlement_ID: 0,
        order_time: '',
        payment_time: '',
        sort: '+id'
      },
      list: null,
      orderAdjustPriceDialogVisible: false,
      total: 0,
      orderForm: {

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleShow(row) {
      this.$router.push({ path: 'order-detail', query: { 'orderId': row.ID }})
    },
    handleCreate() {
      this.$router.push({ path: 'order-add' })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getOrders(this.filterForm).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleSettlement(settlement) {
      this.filterForm.settlement_ID = settlement.ID
      this.filterForm.start = 1
      this.getList()
    },
    resetListQuery() {
      this.filterForm = {
        start: 1,
        perpage: 10,
        keyword: undefined,
        settlement_ID: 0,
        order_time: '',
        payment_time: '',
        sort: '+id'
      }
    },
    handleFilter() {
      this.resetListQuery()
      this.filterForm.start = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      const _this = this
      const formData = {
        'orderId': row.ID,
        'status': status
      }
      changeStatusByOrderId(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const _this = this
      const formData = {
        'orderId': row.ID
      }
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeOrderById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            // _this.filterForm.start = 1
            _this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
